<script lang="ts" setup>
import useStore from '@/store/index'
import { storeToRefs } from 'pinia'
import { ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { CartItem } from '@/types/cart'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
const route = useRoute()
const { goods, cart } = useStore()

watchEffect(() => {
  if (route.fullPath === `/goods/${route.params.id}`) {
    goods.getGoodsInfo(route.params.id as string)
  }
})
const { goodsInfo } = storeToRefs(goods)
let currentSkuId = ''
const changeSku = (skuId: string) => {
  currentSkuId = skuId
  const sku = goodsInfo.value.skus.find((item) => item.id === skuId)
  if (sku) {
    goodsInfo.value.inventory = sku.inventory //更新库存
    goodsInfo.value.price = sku.price //更新价格
    goodsInfo.value.oldPrice = sku.oldPrice //更新老价格
  }
}
//购买数量
const count = ref(1)

//添加购物车
async function addCart() {
  const sku = goodsInfo.value.skus.find((item) => item.id === currentSkuId)
  const attrsText = sku?.specs.map((item) => `${item.name}:${item.valueName}`).join(' ')
  // console.log(sku)

  if (currentSkuId === '') {
    Message.warning('请完善商品规格')
    return
  } else {
    await cart.addCart({
      // 本地添加
      id: goodsInfo.value.id,
      name: goodsInfo.value.name,
      picture: goodsInfo.value.mainPictures[0],
      price: goodsInfo.value.price,
      count: count.value,
      skuId: currentSkuId,
      attrsText: attrsText,
      selected: true,
      nowPrice: goodsInfo.value.price,
      stock: goodsInfo.value.inventory,
      isEffective: true
    } as CartItem)
    Message.success('添加购物车成功')
  }
}
</script>
<template>
  <div class="xtx-goods-page" v-if="goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsInfo.categories[1].id}`">{{
          goodsInfo.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsInfo?.categories[0].id}`">{{
          goodsInfo.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodsInfo.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <GoodName :goods="goodsInfo"></GoodName>
          <GoodsSku @changeSku="changeSku" :goods="goodsInfo"></GoodsSku>
          <!-- 数字选择框 -->
          <XtxNumbox v-model="count"></XtxNumbox>
          <XtxButton type="primary" style="margin: 20px 0 0 10px" @click="addCart">
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsDetail :goods="goodsInfo" />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
